<?php

class RecommendsController extends AppController{
	var $name = 'Recommends';
	var $datahash = array();
	var $components = array('Session','RequestHandler','Twitter');
	var $uses = array('Recommend','Status');

	function beforeFilter(){
		///Ajaxの共通処理
		//アクションにajaxが入っていたら実行
		if( $this->RequestHandler->isAjax() && strstr($this->action,'ajax')){
			//jquery-mobileはすべてajaxなので例外も入れてやる
			Configure::write('debug', 0);
			$this->layout = "";
			$this->autoRender = false;
			$this->RequestHandler->setContent('json');
			$this->RequestHandler->respondAs('application/json; charset=UTF-8');
		}else{
			if( strstr($this->action,'ajax') ){
				$this->redirect('../users/index/');
			}
		}
	}
	
	function beforeRender(){
		$this->set('datahash',$this->datahash);
		parent::beforeRender();
	}
	
	function index(){
		echo "sssss";
	}	
	/*
	function add($userId = null, $statusId = null){
		$this->layout = "";
		$this->autoRender = false;
		if($userId != null && $statusId != null){
			$fav = array('user_id' => $userId, 'status_id' => $statusId);
			$options = array('conditions' => array('Favorite.user_id' => $userId, 'Favorite.status_id' => $statusId));
			$obj = $this->Favorite->find('all',$options);
			if($this->Favorite->save($fav)){
				$this->Session->setFlash("お気に入りに登録しました");
			}else{
				$this->Session->setFlash("お気に入り登録に失敗しました");
			}
		}
		$this->redirect('../users/index/');
	}*/
	
	function del($id = null, $trace = null){
		if($id != null){
			if($this->Reccomend->delete($id)){
				$this->Session->setFlash("お気に入りを解除しました");
			}else{
				$this->Session->setFlash("お気に入りを解除に失敗しました");
			}
		}
		if($trace != null){
			$this->redirect('../users/index/');
		}else{
			$this->redirect("../users/{$trace}/");
		}
	}

	function ajax_addReccomend(){
		$inFavUserId = empty($this->params['form']['in_fav_to_user_id']) ? null : $this->params['form']['in_fav_to_user_id'];
		$userId = empty($this->params['form']['user_id']) ? null : $this->params['form']['user_id'];
		$statusId = empty($this->params['form']['status_id']) ? null : $this->params['form']['status_id'];
		
		if($inFavUserId != null && $userId != null && $statusId != null){
			$data = array(
				'status_id' => $statusId,
				'in_fav_to_user_id' => $inFavUserId,
				'user_id' => $userId
			);
			$options = array('conditions' => array(
				'Recommend.status_id' => $statusId,
				'Recommend.in_fav_to_user_id' => $inFavUserId
			));
			$ret = $this->Recommend->find('all',$options);
			if(empty($ret)){
				if( $this->Recommend->save($data)){
					$values = array("msg" => "GREATを設定しました");
				}else{
					$values = array('msg' => 'Errorがおきました');
				}
			}else{
				$values = array("msg" => "二重にGREATを設定できません");
			}
		}else{
			$values = array('msg' => 'Errorがおきました');
		}
		header("Content-Type: text/javascript; charset=utf-8");
		echo json_encode($values);
		exit();
	}

	function ajax_getMyFavStatuses(){
		$this->Status->recursive = 2;	//アドレスもくっつける
		$count = empty($this->params['form']['count']) ? 10 : $this->params['form']['count'];
		$favId = empty($this->params['form']['favId']) ? 0 : $this->params['form']['favId'];
		if(!empty($user)){
			$values = $this->Favorite->findNextByUserId($user['User']['id'], $favId, $count);
			foreach($values as $key => $val){
				$values[$key] = am($values[$key] ,$this->Status->findById($val['Favorite']['status_id']));
				$values[$key]['TwitterUser'] = $this->Twitter->getTwitterUser($values[$key]);
			}
		}else{
			$values = array('Error' => '1');
		}
		echo json_encode($values);
		exit();
	}
}
?>